package com.aruba.navigation;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class DetailFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_detail, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        HomeFragmentArgs homeFragmentArgs = HomeFragmentArgs.fromBundle(getArguments());
        Toast.makeText(getActivity(),
                homeFragmentArgs.getName() + homeFragmentArgs.getAge(),
                Toast.LENGTH_SHORT).show();

        View button = getView().findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Activity中绑定后，可以通过内部任意一个View获取NavController对象
                NavController navController = Navigation.findNavController(view);
                //指定跳转的fragment，即navigation graph中的action
                navController.navigate(R.id.action_detailFragment_to_homeFragment);
            }
        });
    }
}